package cn.kgc.controller;

import cn.hutool.core.util.StrUtil;
import cn.kgc.base.SystemConstant;
import cn.kgc.entity.Company;
import cn.kgc.feign.UsersFeignClient;
import cn.kgc.form.CompanyAdminReviewForm;
import cn.kgc.form.CompanyForm;
import cn.kgc.form.CompanyQueryForm;
import cn.kgc.form.CompanyReviewForm;
import cn.kgc.grace.result.GraceJSONResult;
import cn.kgc.interceptor.JwtInterceptor;
import cn.kgc.service.CompanyService;
import cn.kgc.utils.PagedGridResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author wangyue
 * @version 1.0
 */
@RestController
@RequestMapping("/company")
@Api(tags = "公司模块")
@Slf4j
@Validated
@SuppressWarnings("all")
public class CompanyController {
    @Resource
    private CompanyService companyService;

    @Resource
    private UsersFeignClient usersFeignClient;

    @PostMapping("/backend/list")
    @ApiOperation(value="管理端分页查询申请成为HR的公司列表")
    public PagedGridResult adminGetCompanyList(@RequestBody CompanyQueryForm companyQueryForm){
        return companyService.queryCompanyList(companyQueryForm);
    }

    @PostMapping("/backend/detail")
    @ApiOperation(value="管理端查看申请信息")
    public GraceJSONResult detail(@RequestParam String companyId){
       return GraceJSONResult.ok(companyService.getById(companyId));
    }

    @PostMapping("/backend/review")
    @ApiOperation(value="管理端审核HR申请")
    public GraceJSONResult review(@Validated @RequestBody CompanyAdminReviewForm  reviewForm){
        Company company = companyService.getById(reviewForm.getCompanyId());
        if(company.getReviewStatus()!=SystemConstant.REVIEW_STATUS_INITIATED){
          return GraceJSONResult.errorMsg("状态为审核中的申请才能被审核");
        }
        //审核HR的求职申请
        return GraceJSONResult.ok(companyService.review(reviewForm));
    }

    @GetMapping("/information")
    @ApiOperation(value = "获得如果用户是HR他所对应的公司信息")
    public GraceJSONResult information(){
        //获得用户的ID
        String userId = JwtInterceptor.userIds.get();
        //获得用户对应的公司的id
        GraceJSONResult graceJSONResult = usersFeignClient.hrDetail(userId);
        //获得远程结果返回的数据
        String companyId=(String) graceJSONResult.getData();
        if(StrUtil.isBlank(companyId)){
            return GraceJSONResult.errorMsg("您还没有申请成为HR,请先申请成为HR");
        }
        //根据公司的id获得公司信息
        Company company=  companyService.getById(companyId);
        //返回公司的信息
        return GraceJSONResult.ok(company);
    }


    @PostMapping("/goReviewCompany")
    @ApiOperation(value = "用户申请成为公司的HR")
    public GraceJSONResult goReviewCompany(@RequestBody CompanyReviewForm form){
        return GraceJSONResult.ok(companyService.goReviewCompany(form));
    }

    @PostMapping("/createCompany")
    @ApiOperation(value = "公司申请入驻招聘平台/重新提交审批")
    public GraceJSONResult createCompany(@RequestBody CompanyForm companyForm){
          if(StrUtil.isBlank(companyForm.getCompanyId())){
              //公司第一次申请入驻,新增公司的信息
              return GraceJSONResult.ok(companyService.createCompany(companyForm));
          }
          //公司重新提交申请,将公司上一次申请的信息清空
         return GraceJSONResult.ok(companyService.reviewCreateCompany(companyForm));
    }

    @PostMapping("/getCompany")
    @ApiOperation(value = "获得公司信息")
    public GraceJSONResult  getCompany(@RequestParam("companyName") String companyName){
        LambdaQueryWrapper<Company> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Company::getCompanyName,companyName);
        Company company= companyService.getOne(queryWrapper);
        return GraceJSONResult.ok(company);
    }
}
